using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.SceneManagement;

public class SceneLoadManager : MonoBehaviour
{
    public FadePanel fadePanel;
    
    private AssetReference currentScene;
    public AssetReference primaryMap;
    public AssetReference intermediateMap;
    public AssetReference seniorMap;
    public AssetReference menu;
    public AssetReference gameMenu;
    public AssetReference intro;
    
    public CardManager cardManager;

    private Vector2Int currentRoomVector;
    private Room currentRoom;
    [Header("广播")]
    public ObjectEventSO afterRoomLoadEvent;
    public ObjectEventSO updateRoomEvent;
    public ObjectEventSO loadMapEvent;
    private void Awake()
    {
        currentRoomVector = Vector2Int.zero * -1;
        LoadMenu();
    }
    public void OnLoadMapEvent()
    {
        loadMapEvent.RaisEvent(null,this);
        if (cardManager.gameLevel == GameLevel.Primary)
        {
            LoadPriamaryMap();
        }
        else if (cardManager.gameLevel == GameLevel.Intermdiate)
        {
            LoadIntermediateMap();
        }
        else if (cardManager.gameLevel == GameLevel.Senior)
        {
            LoadSeniorMap();
        }
    }

   

    /// <summary>
    /// 在房间加载事件中监听
    /// </summary>
    /// <param name="data"></param>
    public async void OnLoadRoomEvent(object data)
    {
        if (data is Room)
        {
            currentRoom = data as Room;
            var currentData = currentRoom.roomDataSO;
            currentRoomVector = new Vector2Int(currentRoom.column, currentRoom.line);
            //Debug.Log("Load Room: " + currentData.name);
            
            currentScene = currentData.sceneToLoad;
        }
        
        //卸载地图
        await UnLoadSceneTask();
        //加载房间
        await LoadSceneTask();
        //刷新房间状态
        afterRoomLoadEvent.RaisEvent(currentRoom, this);
    }

    //新的场景加载方式
    private async Awaitable LoadSceneTask()
    {
        var s = currentScene.LoadSceneAsync(LoadSceneMode.Additive);
        await s.Task;
        if (s.Status == AsyncOperationStatus.Succeeded)
        {
            fadePanel.FadeOut(0.2f);
            SceneManager.SetActiveScene(s.Result.Scene);
        }
           
    }
    
    //新的场景卸载方式
    private async Awaitable UnLoadSceneTask()
    {
        fadePanel.FadeIn(0.4f);
        await Awaitable.WaitForSecondsAsync(0.45f);
        await Awaitable.FromAsyncOperation(SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene()));
    }

    /// <summary>
    /// 监听返回房间的事件函数
    /// </summary>
    public async void LoadPriamaryMap()
    {
        await UnLoadSceneTask();
        if (currentRoomVector != (Vector2.zero * -1))
        {
            updateRoomEvent.RaisEvent(currentRoomVector,this);
        }
        currentScene = primaryMap;
        await LoadSceneTask();
    }
    public async void LoadIntermediateMap()
    {
        await UnLoadSceneTask();
        if (currentRoomVector != (Vector2.zero * -1))
        {
            updateRoomEvent.RaisEvent(currentRoomVector,this);
        }
        currentScene = intermediateMap;
        await LoadSceneTask();
    }
    public async void LoadSeniorMap()
    {
        await UnLoadSceneTask();
        if (currentRoomVector != (Vector2.zero * -1))
        {
            updateRoomEvent.RaisEvent(currentRoomVector,this);
        }
        currentScene = seniorMap;
        await LoadSceneTask();
        
    }

    public async void LoadMenu()
    {
        if (currentScene != null)
            await UnLoadSceneTask();
        currentScene = menu;
        await LoadSceneTask();
    }

    public async void LoadGameMenu()
    {
        if (currentScene != null)
            await UnLoadSceneTask();
        currentScene = gameMenu;
        await LoadSceneTask();
    }
    public async void LoadIntro()
    {
        if (currentScene != null)
            await UnLoadSceneTask();
        currentScene = intro;
        await LoadSceneTask();
    }
}
